<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td><input id="mName" type="text" name="title"/><span id="mNameMsg"></span></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">url</td>
            <td><input id="mUrl" type="text" name="mUrl"/><span id="mUrlMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="status" checked value="0"/> 有效
                <input type="radio" name="status" value="1" /> 被禁用
                <input type="radio" name="status" value="3" /> 未启用
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button type="button" id="add" class="btn btn-primary" type="button">添加</button> &nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    let rId=null;
    function validatamName() {
        var name = $("#mName").val();
        var msg = $("span[id='mNameMsg']");
        $.getJSON("/resource/getmenulist.do",null,function (vo){
            let menuList=vo.list;
            for (let i=0;i<menuList.length;i++){
                if(name==menuList[i].mName){
                    msg.html("资源名重复").css("color", "red");
                    return false;
                }
            }
        });

        if(name == "") {
            msg.html("资源名不能为空").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validatamUrl() {
        var name = $("#mUrl").val();
        var msg = $("span[id='mUrlMsg']");
        if(name == "") {
            msg.html("url不能为空").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataState() {
        var all = $("input[name='status']");
        for(var i = 0; i < all.length; i++) {
            if($(all[i]).prop("checked")) {
                return true;
            }
        }
        alert("状态为必选项");
        return false;
    }

    $(function() {
        $("#mName").blur(function() {
            validatamName();
        });
        $("#mUrl").blur(function() {
            validatamUrl();
        });
    });

    $(function () {
        $(':checkbox[name="group[]"]').click(function () {
            $(':checkbox', $(this).closest('li')).prop('checked', this.checked);
        });

		$('#backid').click(function(){
				window.location.href="/pages/resource/index.html";
		 });

        $("#add").click(function (){
            if (validatamUrl()&&validatamName()&&validataState()){
                let mName=$("#mName").val();
                let mUrl=$("#mUrl").val();
                let mState=$("input[name='status']:checked").val();
                let menu={
                    mName:mName,
                    mUrl:mUrl,
                    mState:mState,
                }

                $.getJSON("/resource/add.do",menu,function (vo){
                    if (vo.code==200){
                        alert("添加成功");
                        window.location.href="/pages/resource/index.html";
                    }else {
                        alert(vo.msg);
                    }
                });
            }
        });
    });

</script>